package com.itheima.controller;

import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.demain.User;
import com.itheima.demain.VO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HttpServletBean;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping(value = "/quick21")
    @ResponseBody
    public void save21(@CookieValue(value = "JSESSIONID") String jsessionId) throws IOException {
        System.out.println(jsessionId);
    }
    @RequestMapping(value = "/quick20")
    @ResponseBody

    public void save20(@RequestHeader(value = "User-Agent",required = false) String user_agent) throws IOException {
        System.out.println(user_agent);
    }
    @RequestMapping(value = "/quick18")
    @ResponseBody
    public void save18(Date date) throws IOException {
        System.out.println(date);
    }
    //localhost:8088/user/quick17/itheima
    @RequestMapping(value = "/quick17/{username}")
    @ResponseBody
    public void save17(@PathVariable(value = "username") String username) throws IOException {
        System.out.println(username);
    }
    @RequestMapping(value = "/quick16")
    @ResponseBody
    public void save16(@RequestParam(value ="name",required = false,defaultValue = "itachi") String username) throws IOException {
        System.out.println(username);
    }
    @RequestMapping(value = "/quick15")
    @ResponseBody
    public void save15(@RequestBody List<User> userList) throws IOException {
        System.out.println(userList);
    }
    @RequestMapping(value = "/quick14")
    @ResponseBody
    public void save14(VO vo) throws IOException {
        System.out.println(vo);
    }
    @RequestMapping(value = "/quick13")
    @ResponseBody
    public void save13(String[] strs) throws IOException {
        System.out.println(Arrays.asList(strs));
    }
    @RequestMapping(value = "/quick12")
    @ResponseBody
    public void save12(User user) throws IOException {
        System.out.println(user);
    }
    @RequestMapping(value = "/quick11")
    @ResponseBody
    public void save11(String username,int age) throws IOException {
        System.out.println(username);
        System.out.println(age);
    }
    @RequestMapping(value = "/quick10")
    @ResponseBody
    //期望SpringMVC自动将User转换成json格式的字符串
    public User save10() throws IOException {
        User user=new User();
        user.setUsername("lisa2");
        user.setAge(13);
        return user;
    }
    @RequestMapping(value = "/quick9")
    @ResponseBody
    public String save9() throws IOException {
        User user=new User();
        user.setUsername("lisa");
        user.setAge(18);
        //使用json的转换工具将对象转换为json格式的字符串
        ObjectMapper objectMapper=new ObjectMapper();
        String json=objectMapper.writeValueAsString(user);
        return json;
    }
    @RequestMapping(value = "/quick8")
    @ResponseBody
    public String save8() throws IOException {
        return "{\"username\":\"zhangsan\",\"age\":18}";
    }
    @RequestMapping(value = "/quick7")
    @ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
    public String save7() throws IOException {
       return "hello cyl";
    }
    @RequestMapping(value = "/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().write("hello,springmvc");
    }
    @RequestMapping(value = "/quick5")
    public String save5(HttpServletRequest request){
       request.setAttribute("username","ddddde");
        return "success";
    }
    @RequestMapping(value = "/quick4")
    public String save4(Model model){
        model.addAttribute("username","decd");
        return "success";
    }
    @RequestMapping(value = "/quick3")
    public ModelAndView save3(ModelAndView mv){
        //设置模型数据
        mv.addObject("username","Angelababy");
        //设置视图名称
        mv.setViewName("success");
        return mv;
    }


    @RequestMapping(value = "/quick2")
    public ModelAndView save2(){
        /*
        Model:模型 作用封装数据
        View:视图  作用展示数据
         */
        ModelAndView mv = new ModelAndView();
        //设置模型数据
        mv.addObject("username","itache");
        //设置视图名称
        mv.setViewName("success");

        return mv;
    }


    //请求地址 http://localhost:8080/quick
    @RequestMapping(value = "/quick",method = RequestMethod.GET,params={"username"})
    public String save(){
        System.out.println("controller......");
        return "success";
    }
}
